home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Games Collection 1 / software vault.zip / software vault / CDR10 / YICN23.ZIP / SOURCE / YEDIT.CPP < prev    next >
C/C++ Source or Header  |  1993-03-06  |  10KB  |  316 lines

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "stddefs.h"
  6. #include "icon.h"
  7. #include "xlib_all.h"
  8. #include "yaklib.h"
  9. #include "gadgets.h"
  10. #include "yakpal.h"
  11.  
  12. #define QUIT 1
  13. #define SAVE 2
  14. #define MIRROR 3
  15.  
  16. #define gridWidth 250
  17. #define gridHeight 200
  18. #define nullChar 13
  19.  
  20. byte grid[gridWidth][gridHeight];
  21. int workingWidth, workingHeight;
  22. int xShift = 0, yShift = 0;
  23. int bigXShift = 0, bigYShift = 0;
  24. icon myicon;
  25. int color = 1;
  26.  
  27. unsigned char bg = 0;
  28.  
  29. void bufferToGrid(char * buffer)
  30. {
  31.   int k = 4;
  32.   for (int heightCounter = 0; heightCounter < gridHeight; ++heightCounter)
  33.     for (int widthCounter = 0; widthCounter < gridWidth; ++widthCounter)
  34.       if ((heightCounter < workingHeight) && (widthCounter < workingWidth))
  35.     grid[widthCounter][heightCounter] = buffer[k++];
  36.       else
  37.     grid[widthCounter][heightCounter] = 0;
  38. }
  39.  
  40. void showBigGrid(void)
  41. {
  42.   int i, j;
  43.   for(i=0;i<32;i++)
  44.     for(j=0;j<32;j++)
  45.     {
  46.       if (grid[i+xShift][j+yShift])
  47.     x_rect_fill(i*5+1,j*5+1,i*5+5,j*5+5,Page0_Offs,grid[i+xShift][j+yShift]);
  48.       else
  49.     x_rect_fill(i*5+1,j*5+1,i*5+5,j*5+5,Page0_Offs,bg);
  50.     }
  51.  
  52.   x_rect_fill(149,174,277,240,Page0_Offs,bg);
  53.   for (i=0; i<128; i++)
  54.     for (j=0;j<64;j++)
  55.     {
  56.       if (grid[i+bigXShift][j+bigYShift])
  57.     x_rect_fill(150+i,175+j,151+i,176+j,Page0_Offs,grid[i+bigXShift][j+bigYShift]);
  58.     }
  59.   x_line(149 + xShift - bigXShift, 174 + yShift - bigYShift, 182 + xShift - bigXShift, 174 + yShift - bigYShift, 8, Page0_Offs);
  60.   x_line(149 + xShift - bigXShift, 208 + yShift - bigYShift, 182 + xShift - bigXShift, 208 + yShift - bigYShift, 8, Page0_Offs);
  61.   x_line(149 + xShift - bigXShift, 174 + yShift - bigYShift, 149 + xShift - bigXShift, 208 + yShift - bigYShift, 8, Page0_Offs);
  62.   x_line(182 + xShift - bigXShift, 174 + yShift - bigYShift, 182 + xShift - bigXShift, 208 + yShift - bigYShift, 8, Page0_Offs);
  63.  
  64. }
  65.  
  66. void shiftGrid(int deltaX, int deltaY)
  67. {
  68.   char spareGrid[gridWidth][gridHeight];
  69.   int widthCounter, heightCounter;
  70.   for (heightCounter = 0; heightCounter < gridHeight; ++heightCounter)
  71.     for (widthCounter = 0; widthCounter < gridWidth; ++widthCounter)
  72.       spareGrid[widthCounter][heightCounter] =
  73.     (((widthCounter + deltaX) < 0) || ((widthCounter + deltaX) >= gridWidth) ||
  74.     (((heightCounter + deltaY) < 0) || ((heightCounter + deltaY) >= gridHeight))) ?
  75.     0 :
  76.     grid[widthCounter + deltaX][heightCounter + deltaY];
  77.   for (heightCounter = 0; heightCounter < gridHeight; ++heightCounter)
  78.     for (widthCounter = 0; widthCounter < gridWidth; ++widthCounter)
  79.       grid[widthCounter][heightCounter] = spareGrid[widthCounter][heightCounter];
  80.   showBigGrid();
  81. }
  82.  
  83. void lookRel(int deltaX, int deltaY)
  84. {
  85.   xShift = ((xShift + deltaX < 0) || (xShift + deltaX + 32 >= gridWidth)) ?
  86.        xShift : xShift + deltaX;
  87.   yShift = ((yShift + deltaY < 0) || (yShift + deltaY + 32 >= gridHeight)) ?
  88.        yShift : yShift + deltaY;
  89.   bigXShift = xShift - (xShift % 64);
  90.   bigYShift = yShift - (yShift % 64);
  91.   showBigGrid();
  92.  
  93. }
  94.  
  95. char * gridToBuffer(void)
  96. {
  97.   char * buffer;
  98.   buffer = new char[4 + workingWidth*workingHeight];
  99.   *(int *)&buffer[0] = (int)workingWidth;
  100.   *(int *)&buffer[2] = (int)workingHeight;
  101.  
  102.   int k = 4;
  103.   for (int heightCounter = 0; heightCounter < gridHeight; ++heightCounter)
  104.     for (int widthCounter = 0; widthCounter < gridWidth; ++widthCounter)
  105.       if ((heightCounter < workingHeight) && (widthCounter < workingWidth))
  106.     buffer[k++] = grid[widthCounter][heightCounter];
  107.   return buffer;
  108. }
  109.  
  110. void getNewWorkingDimensions(void)
  111. {
  112.   workingHeight = workingWidth = 0;
  113.   for (int heightCounter = 0; heightCounter < gridHeight; ++heightCounter)
  114.     for (int widthCounter = 0; widthCounter < gridWidth; ++widthCounter)
  115.       if(grid[widthCounter][heightCounter])
  116.       {
  117.     if (widthCounter >= workingWidth)
  118.       workingWidth = widthCounter + 1;
  119.     if (heightCounter >= workingHeight)
  120.       workingHeight = heightCounter + 1;
  121.       }
  122. }
  123.  
  124. void saveThis(char * filename)
  125. {
  126.   getNewWorkingDimensions();
  127.   char * myRolledBuffer = gridToBuffer();
  128.   delete myicon.unrolledPic;
  129.   myicon.unrolledPic = unRollBlt(myRolledBuffer, workingWidth, workingHeight);
  130.   myicon.width = workingWidth;
  131.   myicon.height = workingHeight;
  132.   myicon.save(filename);
  133. }
  134.  
  135. int gridx(int x)
  136. {
  137.   return x/5 + xShift;
  138. }
  139.  
  140. int gridy(int y)
  141. {
  142.   return y/5 + yShift;
  143. }
  144.  
  145. int gridScreenx(int x)
  146. {
  147.   return (x + xShift) * 5;
  148. }
  149.  
  150. int gridScreeny(int y)
  151. {
  152.   return (y + yShift) * 5;
  153. }
  154.  
  155. int palScreenx(int color)
  156. {
  157.   return (color / 16) * 10 + 200;
  158. }
  159.  
  160. int palScreeny(int color)
  161. {
  162.   return (color % 16) * 10;
  163. }
  164.  
  165. void mirrorGridRight(int column)
  166. {
  167.   int heightCounter, widthCounter;
  168.   for(heightCounter = 0; heightCounter < gridHeight; ++heightCounter)
  169.     for(widthCounter = 0; widthCounter < column; ++widthCounter)
  170.       grid[column + (column - widthCounter - 1)][heightCounter] = grid[widthCounter][heightCounter];
  171.   showBigGrid();
  172. }
  173.  
  174. void selectColor(int tempcolor)
  175. {
  176.   x_rect_fill(palScreenx(color), palScreeny(color), palScreenx(color) + 10, palScreeny(color) + 10, Page0_Offs,0);
  177.   x_rect_fill(palScreenx(color), palScreeny(color), palScreenx(color) + 9, palScreeny(color) + 9, Page0_Offs,color);
  178.   color = tempcolor;
  179.   x_rect_fill(palScreenx(color), palScreeny(color), palScreenx(color) + 10, palScreeny(color) + 10, Page0_Offs,15);
  180.   x_rect_fill(palScreenx(color), palScreeny(color), palScreenx(color) + 9, palScreeny(color) + 9, Page0_Offs,color);
  181. }
  182.  
  183.  
  184. void main(int argc,char *argv[])
  185. {
  186.     int i,j;
  187.     int ext = 0;
  188.     int w,h;
  189.     int x,y,v;
  190.     char commandChar = nullChar;
  191.     char c;
  192.  
  193.     if(argc != 2)
  194.     {
  195.       cout << "\nYakEdit 1.4 modified by V. Putz 6 mar 1993\n\n";
  196.       cout << "usage:\nC:>yedit <filename>\n";
  197.       cout << "where <filename> is a yak icon file.  Include \".yak\" extension.\n\n";
  198.       exit(0);
  199.     }
  200.     x_set_mode(3,376);
  201.     x_text_init();
  202.     yakPalette myYakPalette("standard.ypl");
  203.     myYakPalette.put();
  204.  
  205.     mouse.init();
  206.     yakLib myYakLib("draw");
  207.     gadgetList myGadgetList;
  208.     myGadgetList.add(new gadgetNode(new button(0,20, 'Q', QUIT, "qbutton", icon::normal, &myYakLib),
  209.                      new gadgetNode(new button(0,0, 'S', SAVE, "sbutton", icon::normal, &myYakLib),
  210.                      new gadgetNode(new button(0,40, 'M', MIRROR, ""),NULL))));
  211.     myicon.load(argv[1]);
  212.     char * myRolledBuffer = rollBlt(myicon.unrolledPic, myicon.width, myicon.height);
  213.     workingWidth = myicon.width;
  214.     workingHeight = myicon.height;
  215.     bufferToGrid(myRolledBuffer);
  216.     delete(myRolledBuffer);
  217.     mouse.hide();
  218.     for(i=0;i<16;i++)
  219.     for(j=0;j<16;j++)
  220.         x_rect_fill(200+i*10,j*10,200+i*10+9,j*10+9,Page0_Offs,i*16+j);
  221.     showBigGrid();
  222.     for(i=0;i<33;i++)
  223.     {
  224.     x_line(i*5,0,i*5,160,3,Page0_Offs);
  225.     x_line(0,i*5,160,i*5,3,Page0_Offs);
  226.     }
  227.     myGadgetList.draw(300,190);
  228.     mouse.show();
  229.     while(ext != QUIT)
  230.     {
  231.     x_rect_fill(0,200,20,220,Page0_Offs,color);
  232.  
  233.       mouse.show();
  234.     while(!kbhit() && (mouse.buttonStatus() == 0) && (ext != QUIT))
  235.     {
  236.       x_bgprintf(0,166, Page0_Offs, 20, 1, "X: %d   Y: %d   ", (int)(mouse.x()/5) + xShift, (int)(mouse.y()/5 + yShift));
  237.       ext = myGadgetList.status();
  238.       switch(ext)
  239.         {
  240.           case QUIT : break;
  241.           case SAVE : saveThis(argv[1]); break;
  242.           case MIRROR: mirrorGridRight(gridx(mouse.x())); break;
  243.         }
  244.     }
  245.     mouse.hide();
  246.     x = mouse.x();
  247.     y = mouse.y();
  248.     if (kbhit())
  249.       commandChar = getch();
  250.     if(x > 200 && x < 360 && y > 0 && y < 160)
  251.     {
  252.         if(mouse.isPressed(yakMouse::leftButton))
  253.           selectColor(((x - 200)/10)*16+(y/10));
  254.         else if (mouse.isPressed(yakMouse::rightButton))
  255.         {
  256.           bg = ((x - 200)/10)*16+(y/10);
  257.           showBigGrid();
  258.           x_rect_fill(0,200,20,220,Page0_Offs,color);
  259.         }
  260.     }
  261.     else if(x < 160 && y < 160)
  262.     {
  263.         if(mouse.isPressed(yakMouse::leftButton))
  264.         {
  265.         x_rect_fill(x/5*5+1,y/5*5+1,x/5*5+5,y/5*5+5,Page0_Offs,color);
  266.         x_rect_fill(150+x/5+xShift-bigXShift,175+y/5+yShift-bigYShift,
  267.                 151+x/5+xShift-bigXShift,176+y/5+yShift-bigYShift,Page0_Offs,color);
  268.         grid[gridx(x)][gridy(y)] = color;
  269.         }
  270.         else if (mouse.isPressed(yakMouse::rightButton))
  271.         {
  272.         x_rect_fill(x/5*5+1,y/5*5+1,x/5*5+5,y/5*5+5,Page0_Offs,bg);
  273.         x_rect_fill(150+x/5+xShift-bigXShift,175+y/5+yShift-bigYShift,
  274.                 151+x/5+xShift-bigXShift,176+y/5+yShift-bigYShift,Page0_Offs,bg);
  275.         grid[gridx(x)][gridy(y)] = 0;
  276.         }
  277.  
  278.     }
  279.     if (commandChar != nullChar)
  280.     {
  281.       switch (commandChar)
  282.       {
  283.         case '1' : shiftGrid(1,-1); break;
  284.         case '2' : shiftGrid(0,-1); break;
  285.         case '3' : shiftGrid(-1,-1); break;
  286.         case '4' : shiftGrid(1,0); break;
  287.         case '6' : shiftGrid(-1,0); break;
  288.         case '7' : shiftGrid(1,1); break;
  289.         case '8' : shiftGrid(0,1); break;
  290.         case '9' : shiftGrid(-1,1); break;
  291.             case ' ': selectColor(grid[gridx(x)][gridy(y)]);
  292.           }
  293.           if (commandChar == 0)
  294.           {
  295.         commandChar = getch();
  296.             switch (commandChar)
  297.             {
  298.               case 72 : lookRel(0, -1); break;
  299.           case 80 : lookRel(0, 1); break;
  300.           case 75 : lookRel(-1,0); break;
  301.           case 77 : lookRel(1,0); break;
  302.           case 71 : lookRel(-1,-1); break;
  303.           case 73 : lookRel(1,-1); break;
  304.           case 79 : lookRel(-1,1); break;
  305.           case 81 : lookRel(1,1); break;
  306.             }
  307.       }
  308.       commandChar = 13;
  309.     }
  310.     }
  311.  
  312.     mouse.remove();
  313.     x_text_mode();
  314. }
  315.  
  316.